package com.leo.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Random;

public class Producer {

    private final static String[] LOG_LEVEL = {
            "ERROR",
            "FATAL",
            "WARN"
    };

    private static Random random = new Random();

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri("amqp://root:123456@192.168.56.204:5672/%2f");

        final Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();

        /**
         * 声明direct类型的交换器，交换器和消息队列的绑定不需要在这里处理, exchange-queue绑定关系由消费者处理
         * 由消费者自己声明队列（自带饭盒）
         */
        channel.exchangeDeclare("ex.routing", "direct", false, false, null);

        for (int i = 0; i < 100; i++) {
            String level_rk = LOG_LEVEL[random.nextInt(100) % LOG_LEVEL.length];
            channel.basicPublish("ex.routing", level_rk, null, ("这是【" + level_rk + "】的消息").getBytes());
        }
    }
}